package com.zyw.service.procurement.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyw.entity.*;
import com.zyw.from.ProcurementFrom;
import com.zyw.mapper.procurement.ProcurementMaterialAnalysisMapper;
import com.zyw.mapper.procurement.ProcurementPurchaseOrdersMapper;
import com.zyw.mapper.repertory.RepertoryMaterialMapper;
import com.zyw.service.procurement.ProcurementMaterialAnalysisService;
import com.zyw.service.procurement.ProcurementOrdersService;
import com.zyw.service.procurement.ProcurementPurchaseOrdersService;
import com.zyw.service.repertory.RepertoryMaterialLogService;
import com.zyw.util.UnitConversionUtil;
import com.zyw.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.zyw.constant.ManagermentSystemConstant.MATERIAL_REPERTORY_NUM_MAP;


@Service
@Slf4j
public class ProcurementPurchaseOrdersServiceImpl implements ProcurementPurchaseOrdersService {

    @Resource
    public ProcurementPurchaseOrdersMapper procurementPurchaseOrdersMapper;
    @Autowired
    public ProcurementMaterialAnalysisService procurementMaterialAnalysisService;
    @Resource
    public ProcurementMaterialAnalysisMapper procurementMaterialAnalysisMapper;

    @Resource
    public RepertoryMaterialMapper repertoryMaterialMapper;

    @Autowired
    public ProcurementOrdersService procurementOrdersService;
    @Autowired
    public RepertoryMaterialLogService repertoryMaterialLogService;


    @Override
    @Transactional
    public void saveOrders(String id) {
        ProcurementFrom procurementFrom = new ProcurementFrom();
        procurementFrom.setOrderId(id);
        procurementFrom.setLiningType(null);
        Map<String ,Object> map = new HashMap<>();
        map.put("page",1);
        map.put("limit",10);
        map.put("from",procurementFrom);

        LayUITableVO<List<ProcurementMaterialAnalysis>> vo = procurementMaterialAnalysisService.findPage(map);

        List<ProcurementMaterialAnalysis> dataList = vo.getData();

        List<ProcurementPurchaseOrders> procurementPurchaseOrdersList = new ArrayList<>();

        for (ProcurementMaterialAnalysis procurementMaterialAnalysis : dataList) {
            ProcurementPurchaseOrders procurementPurchaseOrders = new ProcurementPurchaseOrders();
            BeanUtils.copyProperties(procurementMaterialAnalysis,procurementPurchaseOrders);
            procurementPurchaseOrders.setGeneratedDate(UnitConversionUtil.nowDate());
            procurementPurchaseOrders.setStatus(0);
            procurementPurchaseOrdersList.add(procurementPurchaseOrders);
        }

        procurementPurchaseOrdersMapper.saveList(procurementPurchaseOrdersList);

        ProcurementOrders procurementOrders = new ProcurementOrders();
        procurementOrders.setId(id);
        procurementOrders.setProcurementStatus(1);
        procurementOrdersService.updateStatus(procurementOrders);


    }

    @Override
    public LayUITableVO<List<ProcurementPurchaseOrders>> findPage(Map<String, Object> map) {


        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));


        List<ProcurementPurchaseOrders> list = procurementPurchaseOrdersMapper.findPage(map);

        for (ProcurementPurchaseOrders procurementPurchaseOrders : list) {
            procurementPurchaseOrders.setGeneratedDate(UnitConversionUtil.conversionDate(procurementPurchaseOrders.getGeneratedDate()));
            procurementPurchaseOrders.setStorageDate(UnitConversionUtil.conversionDate(procurementPurchaseOrders.getStorageDate()));

            if (procurementPurchaseOrders.getStatus() == 0){
                procurementPurchaseOrders.setStatusString("采购中");
            }else {
                procurementPurchaseOrders.setStatusString("已入库");
            }

        }

        PageInfo<ProcurementPurchaseOrders> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<ProcurementPurchaseOrders>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;
    }

    @Override
    public ProcurementPurchaseOrders selectById(String id) {
        return procurementPurchaseOrdersMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void updateNum(ProcurementPurchaseOrders procurementPurchaseOrders) {

//        Example example = new Example(ProcurementMaterialAnalysis.class);
//        Example.Criteria c = example.createCriteria();
//        c.andEqualTo("orderId",procurementPurchaseOrders.getOrderId());
//        c.andEqualTo("liningName",procurementPurchaseOrders.getLiningName());
//
//        List<ProcurementMaterialAnalysis> procurementMaterialAnalyses = procurementMaterialAnalysisMapper.selectByExample(example);
//        double difference = (procurementPurchaseOrders.getLiningNum() - procurementMaterialAnalyses.get(0).getLiningNum()) * Double.valueOf(procurementMaterialAnalyses.get(0).getLiningPrice());
//
//        procurementMaterialAnalyses.get(0).setLiningNum(procurementPurchaseOrders.getLiningNum());
//        procurementMaterialAnalyses.get(0).setLiningSubtotal(String.valueOf(procurementPurchaseOrders.getLiningNum()*Double.valueOf(procurementMaterialAnalyses.get(0).getLiningPrice())));
//        procurementMaterialAnalysisMapper.updateByExampleSelective(procurementMaterialAnalyses.get(0),example);
//
//        ProcurementOrders procurementOrders = procurementOrdersService.selectById(procurementPurchaseOrders.getOrderId());
//        procurementOrders.setTotal(String.valueOf(Double.valueOf(procurementOrders.getTotal())+difference));
//        procurementOrdersService.updateStatus(procurementOrders);

        procurementPurchaseOrdersMapper.updateByPrimaryKeySelective(procurementPurchaseOrders);

    }

    @Override
    @Transactional
    public void save(ProcurementPurchaseOrders procurementPurchaseOrders) {
     procurementPurchaseOrdersMapper.insertSelective(procurementPurchaseOrders);
    }

    @Override
    @Transactional
    public void updateLog(ProcurementPurchaseOrders procurementPurchaseOrders) {

        Example example = new Example(RepertoryMaterial.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("materialName",procurementPurchaseOrders.getLiningName());
        List<RepertoryMaterial> repertoryMaterialList = repertoryMaterialMapper.selectByExample(example);

        Integer old = repertoryMaterialList.get(0).getRepertoryNum();
        repertoryMaterialList.get(0).setRepertoryNum(old+procurementPurchaseOrders.getLiningNum());
        repertoryMaterialMapper.updateByPrimaryKeySelective(repertoryMaterialList.get(0));

        RepertoryMaterialLog repertoryMaterialLog = new RepertoryMaterialLog();
        repertoryMaterialLog.setMaterialId(repertoryMaterialList.get(0).getId());
        repertoryMaterialLog.setDate(UnitConversionUtil.nowDate());
        //TODO 登录人名称
        repertoryMaterialLog.setOperator("小绿");
        repertoryMaterialLog.setOperatorType("采购入库");
        repertoryMaterialLog.setBeforeRevision(old);
        repertoryMaterialLog.setChangeNum(procurementPurchaseOrders.getLiningNum());

        repertoryMaterialLogService.save(repertoryMaterialLog);

        procurementPurchaseOrdersMapper.updateByPrimaryKeySelective(procurementPurchaseOrders);
    }
}
